#include "form_progressbar.h"
#include "ui_form_progressbar.h"

#include <QtGui>

#define DF_DEGREE_ROUND             360
#define DF_SECOND_MEASURE           1000
#define DF_IMAGE_SIZE               260

FormProgressbar::FormProgressbar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormProgressbar)
{
    ui->setupUi(this);

    this->resize(DF_IMAGE_SIZE, DF_IMAGE_SIZE);

    m_nSecondsCnt = 0;
    m_nValue = 0;
    m_eumProcessState = E_PS_IDLE;
    m_sPromptInfo = tr("Idle");

    m_pixBg.load(":/images/share/progress/progress_bg.png");
    m_pixFg.load(":/images/share/progress/progress_fg.png");

    ui->pushButton_Test->setVisible(false);
    ui->label_PromptInfo->setVisible(true);
    ui->label_PromptInfo->setText(m_sPromptInfo);
    ui->label_PromptInfo->setStyleSheet("QLabel#label_PromptInfo { font-size:28px; color:#007ffe; background:transparent; }");
    ui->pushButton_Test->setStyleSheet("QPushButton#pushButton_Test { font-size:18px; color:#000000; background:transparent; border-image: url(:/images/share/progress/start_normal.png) } QPushButton#pushButton_Test:pressed { font-size:18px; color:#ffffff; background:transparent; border-image: url(:/images/share/progress/start_click.png); }");

    std::vector<bool> vecConnect;
    vecConnect.push_back(connect(&m_tmrRun, SIGNAL(timeout()), this, SLOT(slotUpdateProgressBar()), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    vecConnect.push_back(connect(this, SIGNAL(signalRunTest(bool, int, int)), this, SLOT(slotRunTest(bool, int, int)), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

FormProgressbar::~FormProgressbar()
{
    qDebug() << Q_FUNC_INFO;
    delete ui;
}

void FormProgressbar::paintEvent(QPaintEvent *)
{
    _drawProgressBySecond(m_nValue);
}

void FormProgressbar::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);

        switch (m_eumProcessState) {
        case E_PS_IDLE:
            m_sPromptInfo = tr("Idle");
            break;
        case E_PS_RUNNING:
            m_sPromptInfo = tr("Running");
            break;
        case E_PS_STOPPING:
            m_sPromptInfo = tr("Stopping");
            break;
        default:
            m_sPromptInfo = "";
            break;
        }
        ui->label_PromptInfo->setText(m_sPromptInfo);

        qDebug() << Q_FUNC_INFO;
        break;
    default:
        break;
    }

    QWidget::changeEvent(e);
}

void FormProgressbar::slotUpdateProgressBar()
{
    if(m_nValue > m_nSecondsCnt) {
        if(E_PS_STOPPING == m_eumProcessState || E_PS_IDLE == m_eumProcessState) {
            m_sPromptInfo = tr("Idle");
            ui->label_PromptInfo->setText(m_sPromptInfo);
        }

        m_tmrRun.stop();
        m_nValue = 0;
        m_bStoped = true;

        emit signalProgressOver(false);
    }
    else {
        m_nValue++;
        update();

//        qDebug() << Q_FUNC_INFO << m_nValue << m_nSecondsCnt - m_nValue;
    }
}

void FormProgressbar::on_pushButton_Test_clicked()
{
    if(m_bStoped) {
//        ui->label_PromptInfo->setText("");

        m_tmrRun.start(DF_SECOND_MEASURE);
    }
    else {
        m_tmrRun.stop();

        emit signalProgressOver(true);
    }

    m_nValue = 0;
    update();
    m_bStoped = !m_bStoped;
}

void FormProgressbar::slotRunTest(bool bStart, int nTimeConsuming, int nState)
{
    switch (nState) {
    case E_PS_IDLE:
        m_eumProcessState = E_PS_IDLE;
        m_sPromptInfo = tr("Idle");
        break;
    case E_PS_RUNNING:
        m_eumProcessState = E_PS_RUNNING;
        m_sPromptInfo = tr("Running");
        break;
    case E_PS_PROCESSING:
        m_eumProcessState = E_PS_PROCESSING;
        m_sPromptInfo = tr("Processing");
        break;
    case E_PS_COMPLITED:
        m_eumProcessState = E_PS_COMPLITED;
        m_sPromptInfo = tr("Complited");
        break;
    case E_PS_STOPPING:
        m_eumProcessState = E_PS_STOPPING;
        m_sPromptInfo = tr("Stopping");
        break;
    default:
        m_eumProcessState = E_PS_NONE;
        m_sPromptInfo = "";
        break;
    }

    if(bStart) {
        this->setValue(nTimeConsuming);

        m_bStoped = true;
        ui->label_PromptInfo->setText(m_sPromptInfo);

        this->on_pushButton_Test_clicked();
    }
    else {
        m_bStoped = false;
        ui->label_PromptInfo->setText(m_sPromptInfo);

        this->on_pushButton_Test_clicked();
    }
}

void FormProgressbar::setValue(int nValue)
{
    m_nSecondsCnt = nValue;
    update();
}

int FormProgressbar::getValue()
{
    return m_nValue;
}

void FormProgressbar::_drawProgressBySecond(int nSec)
{
    int r0 = 130;
    int r1 = 116;
    int r2 = 69;
    int nDegree = 0;
    QString strDisplay = "";

    if(!m_bStoped && 0 <= nSec && m_nSecondsCnt >= nSec) {
        strDisplay = _seconds2Time(m_nSecondsCnt - nSec);
        nDegree = ((float)nSec / (float)m_nSecondsCnt) * DF_DEGREE_ROUND;
    }

    int w = qMin(width(), height());
    float scale = w/DF_IMAGE_SIZE;
    int x0 =- r0;
    int y0 =- r0;
    QRect rect(x0,y0,r0*2,r0*2);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    painter.translate(width()/2,height()/2);
    painter.scale(scale, scale);
    painter.drawPixmap(rect,m_pixBg);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(m_pixFg));
    painter.setBrushOrigin(x0,y0);

    int left =  -r1;
    int top =  -r1;
    QPainterPath p;
    p.moveTo(0, top);
    p.arcTo(left, top, r1*2, r1*2, 90, -nDegree);
    left = -r2;
    top = -r2;
    p.arcTo(left, top, r2*2, r2*2, 90 - nDegree, nDegree);
    p.closeSubpath();
    painter.drawPath(p);

    QPen pen(QColor(0,0x7f,0xfe));
    painter.setPen(pen);
    QFont font("Consolas", 16);
    painter.setFont(font);

    QBrush textcolor(QColor(0,0x7f,0xfe));
    QFontMetrics metrics(font);
    QPainterPath textPath;
    QRect bound = metrics.boundingRect(rect, Qt::AlignCenter, strDisplay);
    textPath.addText(bound.x(), bound.y() + metrics.ascent() + 30, font, strDisplay);
    painter.strokePath(textPath, QPen(Qt::yellow));
    painter.setBrush(textcolor);
    painter.drawPath(textPath);
    //painter.fillPath(textPath, textcolor);
}

/*
 * Function:    _seconds2Time
 * Description: Convert seconds to time
 * Paramer:     int nSeconds - countdown by second
 * Return:      QString - time
 */
QString FormProgressbar::_seconds2Time(int nSeconds)
{
    QString sTime = "";
    int nHours = 0;
    int nMinutes = 0;

    nHours= nSeconds / 3600;
    nSeconds %= 3600;
    nMinutes = nSeconds / 60;
    nSeconds %= 60;

    sTime = QString("%1").arg(nHours, 2, 10, QLatin1Char('0'))
            + ":"
            + QString("%1").arg(nMinutes, 2, 10, QLatin1Char('0'))
            + ":"
            + QString("%1").arg(nSeconds, 2, 10, QLatin1Char('0'));

//    qDebug() << Q_FUNC_INFO << nSeconds << sTime;
    return sTime;
}
